<?php
class DoAction extends Ac {
	
	public $fc = "";
	public function __construct(){
		parent::__construct();
		$this->fc = array_merge($_POST,$_GET);
	}
	
	/*
	*	observer
	*/
	public function obs(){
		$data = $this->fc;
		$cls = $data['cls'];
		import('@.Class.Observer');
		$O = new Observer();
		if(strpos($cls,'|')){
			$cls=explode('|',$cls);
			foreach($cls as $_cls){
				if(!class_exists($_cls)) continue;
				$O->attach(new $_cls());
			}
		}else $O->attach(new $cls());
		$O->execute($data);	
		$res = $O->getStatus();
		if($res[0]){	
			$this->success($res[1],$res[2]);
		}else{
			$this->error($res[1],$_SERVER["HTTP_REFERER"]);
		}
	}
	
	/*
	*	公共添加
	*/
	public function add(){
		$data = $this->fc;
		$model = $data['model'];
		if(empty($model)){
			$this->error('缺少参数!');
			return false;
		}
		$obj = M($model);			
		$data['add_time'] = time();
		if($obj->add($data)){	
			$this->success('新增成功', $data['callback']);
		}else{
			$this->error('新增失败');
		}
	}
	
	/*
	*	公共编辑
	*/
	public function edit(){
		$data = $this->fc;
		$model = $data['model'];
		if(empty($model)){
			$this->error('缺少参数!');
			return false;
		}
		$obj = M($model);			
		$res = $obj->where($data['where'])->save($data);
		if($res){
			$this->success('修改成功', $data['callback']);
		}else{
			$this->error('修改失败');
		}	
	}	
		
	/*
	*	公共删除(删除单个)
	*/
	public function del(){
		$id    = $this->_get('id');
		$model = $this->_get('model');
		if(!is_numeric($id)||empty($model)){
			$this->error('缺少参数!');
			return false;
		}
		$obj = M($model);
		$res = $obj->delete($id);
		if($res){
			$this->success('删除成功');
		}else{
			$this->error($obj->getError());
		}	
	}
	
	/*
	*	公共删除(删除多个)
	*/	
	public function dels(){
		$id    = $this->_get('id');
		$model = $this->_get('model');
		if(empty($id)||empty($model)){
			$this->error('缺少参数!');
			return false;
		}
		$obj = M($model);
		$res = $obj->where('id in('.$id.')')->delete();
		if($res){
			$this->success('删除成功');
		}else{
			$this->error($obj->getError());
		}		
	}
	
	/*
	*	数据转移
	*/
	public function move(){
		$id    = $this->_get('id');
		$model = $this->_get('model');
		$cid = $this->_get('cid');
		if(empty($id)||empty($model)||empty($cid)){
			$this->error('缺少参数!');
			return false;
		}
		$data['cid'] = $cid;
		$obj = M($model);
		$res = $obj->where('id in('.$id.')')->save($data);
		if($res){
			$this->success('转移成功');
		}else{
			$this->error($obj->getError());
		}
	}
	
	/*
	*	状态切换
	*/	
	public function status(){	
		$where  = $this->_get('where');
		$model  = $this->_get('model');
		$fields = $this->_get('fields');
		$data   = $this->_get('data');
		if(empty($where)||empty($model)||empty($fields)||empty($data)){
			$this->ajaxReturn('缺少参数!');
			return false;
		}
		$obj = M($model);
		$arr[$fields]=$data;	
		$res = $obj->where($where)->save($arr);
		if($res){
			$this->ajaxReturn($data);
		}else{
			$this->ajaxReturn(0);
		}		
	}
	
	/*
	*	判断唯一
	*/
	public function checkonce(){
		$id 	= $this->_get('id');
		$model  = $this->_get('model');
		$fields = $this->_get('fields');
		$data   = $this->_get('data');
		if(empty($model)||empty($fields)||empty($data)){
			$this->ajaxReturn('缺少参数!');
			return false;
		}
		if(!empty($id)){
			$where = ' and '.$id;
		}	
		$obj = M($model);
		$res = $obj->where("{$fields}='{$data}'".$where)->find();	
		if($res){
			echo 0;
		}else{
			echo 1;
		}
	}
}
?>